<?php
/**
 *	Main Class of Motrada V2 to realize Actions and build Views.
 *	@package		mv2
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			11.12.2006
 *	@version		0.3
 */
/**
 *	Main Class of Motrada V2 to realize Actions and build Views.
 *	@package		mv2
 *	@uses			Net_HTTP_Request_Receiver
 *	@uses			Net_HTTP_Request_Response
 *	@uses			Net_HTTP_PartitionSession
 *	@uses			Database_PDO_Connection
 *	@uses			Framework_Krypton_Core_Registry
 *	@uses			Framework_Krypton_Core_Messenger
 *	@uses			Framework_Krypton_Core_Language
 *	@uses			Framework_Krypton_Core_FormDefinitionReader
 *	@uses			Framework_Krypton_Core_CategoryFactory
 *	@uses			Core_Platform
 *	@uses			Core_PageController
 *	@uses			Logic_Authentication
 *	@uses			View_Interface
 *	@uses			View_Content
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			11.12.2006
 *	@version		0.3
 */
class WebApplication extends Framework_Krypton_Base
{
	/**
	 *	Creates registered Objects and loads Configuration.
	 *	@access		public
	 *	@param		string		$configPath		Path to Configuration Files
	 *	@return		void
	 */
	public function __construct( $configPath = "config/" )
	{
		//  --  ENVIRONMENT  --  //
		$this->initRegistry();										//  must be first
		$this->initConfiguration();									//  must be one of the first
		$this->initEnvironment();									//  must be one of the first
#		$this->initCookie();
		$this->initSession();
		$this->initRequest();

		//  --  RESOURCE SUPPORT  --  //
		$this->initDatabase();										//  needs Configuration
		$this->initLanguage();										//  needs Request and Session
		$this->initFormDefinition();								//  needs Configuration
#		$this->initThemeSupport();									//  needs Configuration, Request and Session
		$this->initPageController();								//  needs Configuration

		//  --  AUTHENTICATION LOGIC  --  //
		$this->initAuthentication();								//  needs Implementation of Authentication Logic in Project Classes
#		$this->initUserInterface();

		ob_start();
		if( !defined( 'CMC_KRYPTON_LIVE_MODE' ) )
			define( 'CMC_KRYPTON_LIVE_MODE', 1 );
	}

	/**
	 *	Runs called Actions.
	 *	@access		public
	 *	@return		void
	 */
	public function act()
	{
		$request	= $this->registry->get( "request" );
		$auth		= $this->registry->get( "auth" );
		$controller	= $this->registry->get( "controller" );

		$link		= $request->get( 'link' );
		if( !$auth->hasAccessToPage( $link ) )
			$link	= $auth->getFirstAccessiblePage();
		$request->set( 'link', $link );

		if( !($controller->checkPage( $link ) && $controller->isDynamic( $link ) ) )
			return;

		$classname	= "Action_".$controller->getClassname( $link );
		$object	= Alg_Object_Factory::createObject( $classname );
		$object->performActions();
		$object->act();
	}

	/**
	 *	Creates Views by called Link and Rights of current User and returns HTML.
	 *	@access		public
	 *	@return		string
	 */
	public function respond()
	{
		$config		= $this->registry->get( "config" );
		$session	= $this->registry->get( "session" );
		$request	= $this->registry->get( "request" );
		$messenger	= $this->registry->get( "messenger" );
		$language	= $this->registry->get( "language" );
		$words		= $this->registry->get( "words" );
		$controller	= $this->registry->get( "controller" );
		$st			= $this->registry->get( "stopwatch" );
//		$auth		= $this->registry->get( "auth" );
		$extra		= "";
		$field		= "";
		$control	= "";
		$content	= "";
		$link		= $request->get( 'link' );

//		$view		= new View_Auth;
//		$control	= $view->buildControl();
//		$content	= $view->buildContent();
	
		if( !$content )
		{
			if( $controller->checkPage( $link, 'main' ) )
			{
				if( $controller->isDynamic( $link ) )
				{
					$classname	= "View_".$controller->getClassname( $link );
					$view		= Alg_Object_Factory::createObject( $classname );
					$content	= $view->buildContent();
					$control	.= $view->buildControl();
				//	$extra		.= $view->buildExtra();
				}
				else
				{
					$source		= $controller->getSource( $link );
					remark( "<b>HTML File: </b>".$source );
					$view		= new View_Content;
					$content	= $view->loadContent( $source );
				//	$content	= $view->buildContent();
				//	$extra		.= $view->buildExtra();
				}
			}
		}

		$ui['title']	= $words['main']['main']['title'];
		$ui['path_js']	= $config['paths']['javascripts'];
		$ui['path_css']	= $config['paths']['themes'].$config['layout']['theme']."/css/";
		$ui['layout']	= $config['layout']['style'];

		$view	= new View_Interface();

		$ui['control']			= $control;
		$ui['content']			= $content;
		$ui['extra']			= $extra;
		$ui['javascript']		= file_get_contents( "contents/javascripts/jquery.plugins.pack.js" );
		$ui['mainheader']		= $view->buildMainHeader();
		$ui['subheader']		= $view->buildSubHeader();
		$ui['mainnavigation']	= $view->buildMainNavigation();
		$ui['subnavigation']	= $view->buildSubNavigation();
		$ui['mainfooter']		= $view->buildMainFooter();
		$ui['subfooter']		= $view->buildSubFooter();
		$ui['messages']			= $messenger->buildMessages();
		$ui['dev']				= $view->buildDevCenter( ob_get_clean() );
		$ui['language']			= $language->getLanguage();
		$ui['config']			= $config;
		try
		{
			$content	= $view->loadTemplate( 'master', $ui, ".", "/", true );
			$content	= str_replace( "[[%", "<%", $content );
			$content	= str_replace( "%]]", "%>", $content );
		}
		catch( Exception $e )
		{
			$labels	= implode( ", ", $e->getNotUsedLabels() );
			$labels	= htmlentities( $labels );
			die( $e->getMessage()."<br/><small>".$labels."</small>" );
		}
		$response	= new Net_HTTP_Request_Response();
		$response->write( $content );
		$response->send();
	}
}
?>